/*
 * @文件描述: 
 * @版本: 
 * @作者: 周晨阳
 * @Date: 2021-02-13 22:28:25
 */
#ifdef GUI_UCGUI
#include "cursorWindow.h"
#include <stddef.h>
#include "GUI.h"
#include "DIALOG.h"

#include "WM.h"
#include "BUTTON.h"
#include "CHECKBOX.h"
#include "DROPDOWN.h"
#include "EDIT.h"
#include "FRAMEWIN.h"
#include "LISTBOX.h"
#include "MULTIEDIT.h"
#include "RADIO.h"
#include "SLIDER.h"
#include "TEXT.h"
#include "PROGBAR.h"
#include "SCROLLBAR.h"
#include "LISTVIEW.h"

/*********************************************************************
*
*       static data
*
**********************************************************************
*/
static Cursor *Cursors;
static OscillInfo *LeftsideInfo;
//EventsFunctionList
void OnY2CloseClicked(WM_MESSAGE *pMsg);
void OnY1CloseClicked(WM_MESSAGE *pMsg);
void OnX2CloseClicked(WM_MESSAGE *pMsg);
void OnX1CloseClicked(WM_MESSAGE *pMsg);
void OnX2Clicked(WM_MESSAGE *pMsg);
void OnY2Clicked(WM_MESSAGE *pMsg);
void OnY1Clicked(WM_MESSAGE *pMsg);
void OnX1Clicked(WM_MESSAGE *pMsg);
//EndofEventsFunctionList

/*********************************************************************
*
*       static data
*
**********************************************************************
*/
#define LEFT_INFO_ID_FREQ 0
#define LEFT_INFO_ID_CURA 1
#define LEFT_INFO_ID_CURB 2
#define LEFT_INFO_ID_DX 3
#define LEFT_INFO_ID_FX 4
#define LEFT_INFO_ID_CURC 5
#define LEFT_INFO_ID_CURD 6
#define LEFT_INFO_ID_DY 7

/*********************************************************************
*
*       Dialog resource
*
* This table conatins the info required to create the dialog.
* It has been created by ucGUIbuilder.
*/

static const GUI_WIDGET_CREATE_INFO _aDialogCreate[] = {
    {FRAMEWIN_CreateIndirect, "Cursor", 0, 0, 0, 223, 480, FRAMEWIN_CF_MOVEABLE, 0},
    {TEXT_CreateIndirect, "Type:", GUI_ID_TEXT0, 69, 6, 60, 38, 0, 0},
    {BUTTON_CreateIndirect, "X1", GUI_ID_BUTTON0, 8, 50, 113, 75, 0, 0},
    {BUTTON_CreateIndirect, "Y1", GUI_ID_BUTTON2, 7, 224, 114, 75, 0, 0},
    {BUTTON_CreateIndirect, "Y2", GUI_ID_BUTTON3, 7, 311, 114, 75, 0, 0},
    {BUTTON_CreateIndirect, "X2", GUI_ID_BUTTON4, 8, 137, 113, 75, 0, 0},
    {BUTTON_CreateIndirect, "X", GUI_ID_BUTTON5, 127, 50, 75, 75, 0, 0},
    {BUTTON_CreateIndirect, "X", GUI_ID_BUTTON6, 127, 137, 75, 75, 0, 0},
    {BUTTON_CreateIndirect, "X", GUI_ID_BUTTON7, 127, 224, 75, 75, 0, 0},
    {BUTTON_CreateIndirect, "X", GUI_ID_BUTTON8, 127, 311, 75, 75, 0, 0}};

/*****************************************************************
**      FunctionName:void PaintDialog(WM_MESSAGE * pMsg)
**      Function: to initialize the Dialog items
**                                                      
**      call this function in _cbCallback --> WM_PAINT
*****************************************************************/

void PaintDialog(WM_MESSAGE *pMsg)
{
    WM_HWIN hWin = pMsg->hWin;
    BUTTON_SetBkColor(WM_GetDialogItem(hWin, GUI_ID_BUTTON0), BUTTON_CI_UNPRESSED, 0xe0e0e0);
    BUTTON_SetFont(WM_GetDialogItem(hWin, GUI_ID_BUTTON0), &GUI_Font32_ASCII);

    if (Cursors[0].isActive)
    {
        BUTTON_SetBkColor(WM_GetDialogItem(hWin, GUI_ID_BUTTON0), 0, Cursors[0].color);
    }
    //
    //GUI_ID_BUTTON2 Y1
    //
    BUTTON_SetBkColor(WM_GetDialogItem(hWin, GUI_ID_BUTTON2), BUTTON_CI_UNPRESSED, 0xe0e0e0);
    BUTTON_SetFont(WM_GetDialogItem(hWin, GUI_ID_BUTTON2), &GUI_Font32_ASCII);
    if (Cursors[2].isActive)
    {
        BUTTON_SetBkColor(WM_GetDialogItem(hWin, GUI_ID_BUTTON2), 0, Cursors[2].color);
    }
    //
    //GUI_ID_BUTTON3 Y2
    //
    BUTTON_SetBkColor(WM_GetDialogItem(hWin, GUI_ID_BUTTON3), BUTTON_CI_UNPRESSED, 0xe0e0e0);
    BUTTON_SetFont(WM_GetDialogItem(hWin, GUI_ID_BUTTON3), &GUI_Font32_ASCII);
    if (Cursors[3].isActive)
    {
        BUTTON_SetBkColor(WM_GetDialogItem(hWin, GUI_ID_BUTTON3), 0, Cursors[3].color);
    }
    //
    //GUI_ID_BUTTON4 X2
    //
    BUTTON_SetBkColor(WM_GetDialogItem(hWin, GUI_ID_BUTTON4), BUTTON_CI_UNPRESSED, 0xe0e0e0);
    BUTTON_SetFont(WM_GetDialogItem(hWin, GUI_ID_BUTTON4), &GUI_Font32_ASCII);
    if (Cursors[1].isActive)
    {
        BUTTON_SetBkColor(WM_GetDialogItem(hWin, GUI_ID_BUTTON4), 0, Cursors[1].color);
    }
}

/*****************************************************************
**      FunctionName:void InitDialog(WM_MESSAGE * pMsg)
**      Function: to initialize the Dialog items
**                                                      
**      call this function in _cbCallback --> WM_INIT_DIALOG
*****************************************************************/

void InitDialog(WM_MESSAGE *pMsg)
{
    WM_HWIN hWin = pMsg->hWin;
    //
    //FRAMEWIN
    //
    FRAMEWIN_SetBarColor(hWin, 1, 0x1976d2);
    FRAMEWIN_SetFont(hWin, &GUI_Font24B_1);
    FRAMEWIN_AddCloseButton(hWin, FRAMEWIN_BUTTON_RIGHT, 0);
    FRAMEWIN_SetTitleHeight(hWin, 57);
    //
    //GUI_ID_TEXT0
    //
    TEXT_SetFont(WM_GetDialogItem(hWin, GUI_ID_TEXT0), &GUI_Font24_ASCII);
    //
    //GUI_ID_BUTTON0 X1
    //
    BUTTON_SetBkColor(WM_GetDialogItem(hWin, GUI_ID_BUTTON0), BUTTON_CI_UNPRESSED, 0xe0e0e0);
    BUTTON_SetFont(WM_GetDialogItem(hWin, GUI_ID_BUTTON0), &GUI_Font32_ASCII);
    if (Cursors[0].isActive)
    {
        BUTTON_SetBkColor(WM_GetDialogItem(hWin, GUI_ID_BUTTON0), 0, Cursors[0].color);
    }
    //
    //GUI_ID_BUTTON2 Y1
    //
    BUTTON_SetBkColor(WM_GetDialogItem(hWin, GUI_ID_BUTTON2), BUTTON_CI_UNPRESSED, 0xe0e0e0);
    BUTTON_SetFont(WM_GetDialogItem(hWin, GUI_ID_BUTTON2), &GUI_Font32_ASCII);
    if (Cursors[2].isActive)
    {
        BUTTON_SetBkColor(WM_GetDialogItem(hWin, GUI_ID_BUTTON2), 0, Cursors[2].color);
    }
    //
    //GUI_ID_BUTTON3 Y2
    //
    BUTTON_SetBkColor(WM_GetDialogItem(hWin, GUI_ID_BUTTON3), BUTTON_CI_UNPRESSED, 0xe0e0e0);
    BUTTON_SetFont(WM_GetDialogItem(hWin, GUI_ID_BUTTON3), &GUI_Font32_ASCII);
    if (Cursors[3].isActive)
    {
        BUTTON_SetBkColor(WM_GetDialogItem(hWin, GUI_ID_BUTTON3), 0, Cursors[3].color);
    }
    //
    //GUI_ID_BUTTON4 X2
    //
    BUTTON_SetBkColor(WM_GetDialogItem(hWin, GUI_ID_BUTTON4), BUTTON_CI_UNPRESSED, 0xe0e0e0);
    BUTTON_SetFont(WM_GetDialogItem(hWin, GUI_ID_BUTTON4), &GUI_Font32_ASCII);
    if (Cursors[1].isActive)
    {
        BUTTON_SetBkColor(WM_GetDialogItem(hWin, GUI_ID_BUTTON4), 0, Cursors[1].color);
    }

    //
    //GUI_ID_BUTTON5
    //
    BUTTON_SetBkColor(WM_GetDialogItem(hWin, GUI_ID_BUTTON5), BUTTON_CI_UNPRESSED, 0xdd2c00);
    BUTTON_SetFont(WM_GetDialogItem(hWin, GUI_ID_BUTTON5), &GUI_Font32_ASCII);
    //
    //GUI_ID_BUTTON6
    //
    BUTTON_SetBkColor(WM_GetDialogItem(hWin, GUI_ID_BUTTON6), BUTTON_CI_UNPRESSED, 0xdd2c00);
    BUTTON_SetFont(WM_GetDialogItem(hWin, GUI_ID_BUTTON6), &GUI_Font32_ASCII);
    //
    //GUI_ID_BUTTON7
    //
    BUTTON_SetBkColor(WM_GetDialogItem(hWin, GUI_ID_BUTTON7), BUTTON_CI_UNPRESSED, 0xdd2c00);
    BUTTON_SetFont(WM_GetDialogItem(hWin, GUI_ID_BUTTON7), &GUI_Font32_ASCII);
    //
    //GUI_ID_BUTTON8
    //
    BUTTON_SetBkColor(WM_GetDialogItem(hWin, GUI_ID_BUTTON8), BUTTON_CI_UNPRESSED, 0xdd2c00);
    BUTTON_SetFont(WM_GetDialogItem(hWin, GUI_ID_BUTTON8), &GUI_Font32_ASCII);
}
/**
 * @brief : 用于控制左侧信息框中哪些内容需要显示，是否需要显示取决于用户
 * @param {*}
 * @return {*}
 */
static void leftSideInfoProcess(void)
{

    if (Cursors[0].isVisible == 1 && Cursors[1].isVisible == 1)
    {
        LeftsideInfo[LEFT_INFO_ID_DY].isShowed = 1;
    }
    else
    {
        LeftsideInfo[LEFT_INFO_ID_DY].isShowed = 0;
    }
    if (Cursors[2].isVisible == 1 && Cursors[3].isVisible == 1)
    {
        LeftsideInfo[LEFT_INFO_ID_DX].isShowed = 1;
        LeftsideInfo[LEFT_INFO_ID_FX].isShowed = 1;
    }
    else
    {
        LeftsideInfo[LEFT_INFO_ID_DX].isShowed = 0;
        LeftsideInfo[LEFT_INFO_ID_FX].isShowed = 0;
    }
}
/*********************************************************************
*
*       Dialog callback routine
*/
static void _cbCallback(WM_MESSAGE *pMsg)
{
    int NCode, Id;
    WM_HWIN hWin = pMsg->hWin;
    switch (pMsg->MsgId)
    {
    case WM_PAINT:
        PaintDialog(pMsg);
        break;
    case WM_INIT_DIALOG:
        InitDialog(pMsg);
        break;
    case WM_KEY:
        switch (((WM_KEY_INFO *)(pMsg->Data.p))->Key)
        {
        case GUI_KEY_ESCAPE:
            GUI_EndDialog(hWin, 1);
            break;
        case GUI_KEY_ENTER:
            GUI_EndDialog(hWin, 0);
            break;
        }
        break;
    case WM_NOTIFY_PARENT:
        Id = WM_GetId(pMsg->hWinSrc);
        NCode = pMsg->Data.v;
        switch (Id)
        {
        case GUI_ID_OK:
            if (NCode == WM_NOTIFICATION_RELEASED)
                GUI_EndDialog(hWin, 0);
            break;
        case GUI_ID_CANCEL:
            if (NCode == WM_NOTIFICATION_RELEASED)
                GUI_EndDialog(hWin, 0);
            break;
        case GUI_ID_BUTTON0:
            switch (NCode)
            {
            case WM_NOTIFICATION_CLICKED:
                OnX1Clicked(pMsg);
                Cursors[0].isVisible = 1;
                LeftsideInfo[LEFT_INFO_ID_CURC].isShowed = 1;

                if (Cursors[0].isActive == 1)
                {
                    Cursors[0].isActive = 0;
                    //E0E0E0
                    BUTTON_SetBkColor(WM_GetDialogItem(hWin, GUI_ID_BUTTON0), 0, 0xE0E0E0);
                }
                else
                {
                    Cursors[0].isActive = 1;

                    BUTTON_SetBkColor(WM_GetDialogItem(hWin, GUI_ID_BUTTON0), 0, Cursors[0].color);
                }
                leftSideInfoProcess();
                break;
            }
            break;
        case GUI_ID_BUTTON2:
            switch (NCode)
            {
            case WM_NOTIFICATION_CLICKED:
                OnY1Clicked(pMsg);
                Cursors[2].isVisible = 1;
                LeftsideInfo[LEFT_INFO_ID_CURA].isShowed = 1;
                if (Cursors[2].isActive == 1)
                {
                    Cursors[2].isActive = 0;
                    BUTTON_SetBkColor(WM_GetDialogItem(hWin, GUI_ID_BUTTON2), 0, 0xE0E0E0);
                }
                else
                {
                    Cursors[2].isActive = 1;
                    BUTTON_SetBkColor(WM_GetDialogItem(hWin, GUI_ID_BUTTON2), 0, Cursors[2].color);
                }
                leftSideInfoProcess();
                break;
            }
            break;
        case GUI_ID_BUTTON3:
            switch (NCode)
            {
            case WM_NOTIFICATION_CLICKED:
                OnY2Clicked(pMsg);
                Cursors[3].isVisible = 1;
                LeftsideInfo[LEFT_INFO_ID_CURB].isShowed = 1;
                if (Cursors[3].isActive == 1)
                {
                    Cursors[3].isActive = 0;
                    BUTTON_SetBkColor(WM_GetDialogItem(hWin, GUI_ID_BUTTON3), 0, 0xE0E0E0);
                }
                else
                {
                    Cursors[3].isActive = 1;
                    BUTTON_SetBkColor(WM_GetDialogItem(hWin, GUI_ID_BUTTON3), 0, Cursors[3].color);
                }
                leftSideInfoProcess();

                break;
            }
            break;
        case GUI_ID_BUTTON4:
            switch (NCode)
            {
            case WM_NOTIFICATION_CLICKED:
                OnX2Clicked(pMsg);
                Cursors[1].isVisible = 1;
                LeftsideInfo[LEFT_INFO_ID_CURD].isShowed = 1;
                if (Cursors[1].isActive == 1)
                {
                    Cursors[1].isActive = 0;
                    BUTTON_SetBkColor(WM_GetDialogItem(hWin, GUI_ID_BUTTON4), 0, 0xE0E0E0);
                }
                else
                {
                    Cursors[1].isActive = 1;
                    BUTTON_SetBkColor(WM_GetDialogItem(hWin, GUI_ID_BUTTON4), 0, Cursors[1].color);
                }
                leftSideInfoProcess();
                break;
            }
            break;
        //以下为关闭按钮
        case GUI_ID_BUTTON5:
            switch (NCode)
            {
            case WM_NOTIFICATION_CLICKED:
                OnX1CloseClicked(pMsg);
                Cursors[0].isVisible = 0;
                Cursors[0].isActive = 0;
                LeftsideInfo[LEFT_INFO_ID_CURC].isShowed = 0;
                leftSideInfoProcess();
                PaintDialog(pMsg);
                break;
            }
            break;
        case GUI_ID_BUTTON6:
            switch (NCode)
            {
            case WM_NOTIFICATION_CLICKED:
                OnX2CloseClicked(pMsg);
                Cursors[1].isVisible = 0;
                Cursors[1].isActive = 0;
                LeftsideInfo[LEFT_INFO_ID_CURD].isShowed = 0;
                leftSideInfoProcess();
                PaintDialog(pMsg);

                break;
            }
            break;
        case GUI_ID_BUTTON7:
            switch (NCode)
            {
            case WM_NOTIFICATION_CLICKED:
                OnY1CloseClicked(pMsg);
                Cursors[2].isVisible = 0;
                Cursors[2].isActive = 0;
                LeftsideInfo[LEFT_INFO_ID_CURA].isShowed = 0;
                leftSideInfoProcess();
                PaintDialog(pMsg);

                break;
            }
            break;
        case GUI_ID_BUTTON8:
            switch (NCode)
            {
            case WM_NOTIFICATION_CLICKED:
                OnY2CloseClicked(pMsg);
                Cursors[3].isVisible = 0;
                Cursors[3].isActive = 0;
                LeftsideInfo[LEFT_INFO_ID_CURB].isShowed = 0;
                leftSideInfoProcess();
                PaintDialog(pMsg);

                break;
            }
            break;
        }
        //WM_InvalidateWindow(WM_HBKWIN);
        break;
    default:
        WM_DefaultProc(pMsg);
    }
}

void createCursorWin(void)
{

    GUI_CreateDialogBox(_aDialogCreate, GUI_COUNTOF(_aDialogCreate), &_cbCallback, 0, 0, 0);
}

void setCursor(Cursor *c)
{
    Cursors = c;
}
void setLeftSideInfo(OscillInfo *l)
{
    LeftsideInfo = l;
}
#endif